package de.listrong.da.ontology;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;

import de.listrong.da.ontology.util.WriteToABox;
import de.listrong.da.tools.NewIDGener;
import gate.Annotation;

public class SentenceIndividual extends TextSegment implements
		Comparable<SentenceIndividual>, WriteToABox {

	/*--- private property ---*/
    // each sentence has a wordList to contains word individuals
	private WordList wordList;

	public WordList getWordList() {
		return wordList;
	}

	public void setWordList(WordList wordList) {
		this.wordList = wordList;
	}

	/*--- construct ---*/
	public SentenceIndividual(Annotation anno) {
		this.id = NewIDGener.newId(OntologyClasses.SENTENCE);
                // IdGenerator.getInstance().newId(OntologyClasses.SENTENCE);
		this.beginOffset = anno.getStartNode().getOffset();
		this.endOffset = anno.getEndNode().getOffset();
		this.wordList = null;
	}

    /* public methods*/
	public boolean containTheWord(WordIndividual aWord) {
		if ((aWord.beginOffset >= this.beginOffset)
				&& (aWord.endOffset <= this.endOffset)) {
			return true;
		} else {
			return false;
		}
	}

	public void addWord(WordIndividual aWordElement) {
		if (this.wordList == null) {
			wordList = new WordList();
		}
		this.wordList.add(aWordElement);
	}

	public int compareTo(SentenceIndividual o) {
		if (this.beginOffset < o.beginOffset) {
			return -1;
		} else if (this.beginOffset == o.beginOffset) {
			return 0;
		} else {
			return 1;
		}
	}

    /* override method from Interface*/
	@Override
	public Individual toOntIndiviual(OntModel tbox, OntModel abox) {
		String tBoxDefaultNS = tbox.getNsPrefixURI("");
		String aBoxDefaultNS = abox.getNsPrefixURI("");
		OntClass ontClass = tbox.getOntClass(tBoxDefaultNS + "Sentence");
		
		Individual ind0 = abox.createIndividual(aBoxDefaultNS + "SEN-"
				+ String.valueOf(this.getId()), ontClass);	// create a Individual object
		
		DatatypeProperty propBegin = tbox.getDatatypeProperty(tBoxDefaultNS
				+ "beginAt");
		ind0.addLiteral(propBegin, this.getBeginOffset());  // set "beginAt" DataType Property 
		
		DatatypeProperty propEnd = tbox.getDatatypeProperty(tBoxDefaultNS
				+ "endAt");
		ind0.addLiteral(propEnd, this.getEndOffset());		// set "endAt" DataType Property
		
		if (this.getWordList() != null) {
			for (WordIndividual aWord : this.wordList) {
				Individual ontIndividualWord = aWord.toOntIndiviual(tbox, abox);
				ObjectProperty containProperty = tbox
						.getObjectProperty(tBoxDefaultNS + "contains");
				ind0.addProperty(containProperty, ontIndividualWord);
			}
		}

		return ind0;
	}

	@Override
	public String toString() {
		return "Sentence: id=" + this.id + ", beginOffset=" + this.beginOffset
				+ ", endOffset=" + this.endOffset + "\n" + this.wordList;
	}

}
